<?php

namespace App\Http\Controllers;

use App\Models\Domain;
use App\Models\DwzUrl;
use App\Models\Forward;
use Illuminate\Support\Facades\Hash;
use App\Models\User;
use App\Models\Vip;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class IndexController extends Controller
{
    public function sy(Request $request)
    {
        if ($request->getHost()==admin_setting('host')) {
            return view('index.mifa.index') ;
        }else{
            $forword  = Forward::where('domain',$request->getHost())->first();
            if ($forword) {
              if ($forword->status!=1) {
                return '域名被禁用，或者未审核';
              }
              //显现转发
              if($forword->mode==0){
                header('Location: '.$forword->url);
                exit;
              }
              if($forword->mode==1){
                return view('frame',compact('forword'));
              }
            }else{
                return '不存在';
            }
        }
        
    }
    //短网址跳转
    public function dwz_link(Request $request ,$code)
    {
        $domain=Domain::where('domain',$request->getHost())->first();
        $dwzurl=DwzUrl::where('code',$code)->where('domain_id',$domain->id)->first();
        if ($dwzurl) { 
            header('Location: '.$dwzurl->url);
            //增加访问量+1
            $dwzurl->view=$dwzurl->view+1;
            $dwzurl->save();
                exit;
        }else{
            return '短网址不存在';
        }
    }
    public function login(Request $request)
    {
        //判断是否已经登录
        if (session('user')) {
            return redirect('/user');
        }
        if ($request->isMethod('post')) {
            $email = $request->input('email');
            $pass = $request->input('pass');

            // 验证邮箱和密码不能为空
            if (empty($email) || empty($pass)) {
                return back()->with('danger', '邮箱和密码不能为空');
            }

            // 查找用户
            $user = User::where('email', $email)->first();

            // 验证用户是否存在且密码是否正确
            if ($user && Hash::check($pass, $user->pass)) {
                // 登录成功，设置用户会话
                session(['user' => $user]);
                return redirect('/user')->with('success', '登录成功！');
            } else {
                // 登录失败
                return back()->with('danger', '邮箱或密码错误');
            }
        }
        return view('user.login');
    }
    public function register(Request $request)
    {
        //判断是否已经登录
        if (session('user')) {
            return redirect('/user');
        }
        if ($request->isMethod('post')) {
            $email = $request->input('email');
            $pass = $request->input('pass');
            $captcha = $request->input('captcha');

            // 验证邮箱和密码不能为空且密码长度大于6位
            if (empty($email) || empty($pass)) {
                return back()->with('danger', '邮箱和密码不能为空');
            }
            if (strlen($pass) < 6) {
                return back()->with('danger', '密码长度必须大于6位');
            }

            $storedCode = \Cache::get('verify_code_' . $email);

            if ($storedCode && $storedCode == $captcha) {
                // 验证码正确
               // 检查用户是否已存在
            if (User::where('email', $email)->exists()) {
                return back()->with('danger', '该邮箱已被注册');
            }

            // 创建新用户
            $newAgent = new User();
            $newAgent->email = $email;
            $newAgent->dwz = admin_setting('Default_domain','0');
            $newAgent->forward = admin_setting('Default_forward','10');
            $newAgent->vip_id = admin_setting('Default_vip','1');
            $newAgent->pass = hash::make($pass); // 使用Hash::make加密密码
            $newAgent->api_token = Str::random(60);
            $newAgent->save();

            return  back()->with('success', '注册成功，请登录！');

            } else {
                // 验证码错误或过期
                return back()->with('danger', '验证码错误或过期');
            }
            
        }
        return view('user.register');
    }
    public function index()
    {
        $user=User::where('id',session('user')->id)->first();
        $forword=Forward::where('uid',session('user')->id)->count();
        $domain=Domain::where('uid',session('user')->id)->count();
        $dwz=DwzUrl::where('uid',session('user')->id)->count();
        $vip=Vip::where('id',$user->vip_id)->first();
        $vips = Vip::where('sort', '>', $vip->sort )
           ->orderBy('sort', 'asc') // 按 stort 字段从小到大排序
           ->get();
        return view('user.index',compact('user','forword','domain','dwz','vip','vips'));
    }
    public function forward_list()
    {
        if (isset( $_GET['kw'])) {
            $forword=Forward::where('uid',session('user')->id)->where('DOMAIN','like','%'.$_GET['kw'].'%')->paginate(10);
        }else{
        $forword=Forward::where('uid',session('user')->id)->paginate(10);
        }
        return view('user.forward_list',compact('forword'));
    }
    public function forward_add()
    {
        return view('user.forward_add');
    }
    public function forward_edit($id)
    {
        $forword=forward::where('id',$id)->where('uid',session('user')->id)->first();
        return view('user.forward_edit',compact('forword'));
    }
    public function forward_info($id)
    {
        $Res=forward::where('id',$id)->where('uid',session('user')->id)->first();
        return view('user.forward_info',compact('Res'));
    }
    public function domain_list()
    {
        if (isset( $_GET['kw'])) {
            $domain=Domain::where('uid',session('user')->id)->where('DOMAIN','like','%'.$_GET['kw'].'%')->paginate(10);
        }else{
        $domain=Domain::where('uid',session('user')->id)->paginate(10);
        }
        return view('user.domain_list',compact('domain'));
    }
    public function domain_add()
    {
        
        return view('user.domain_add');
    }
    public function domain_edit($id)
    {
        $domain=domain::where('id',$id)->where('uid',session('user')->id)->first();
        return view('user.domain_edit',compact('domain'));
    }
    public function dwz_list($id)
    {
        if (isset( $_GET['kw'])) {
            $dwz=DwzUrl::where('uid',session('user')->id)->where('domain_id',$id)->where('DOMAIN','like','%'.$_GET['kw'].'%')->paginate(10);
        }else{
        $dwz=DwzUrl::where('uid',session('user')->id)->where('domain_id',$id)->paginate(10);
        }
        $domain=Domain::where('id',$id)->first();
        return view('user.dwz_list',compact('dwz','domain'));
    }
    public function dwz_add($id)
    {
        $domain=Domain::where('id',$id)->where('uid',session('user')->id)->first();
        return view('user.dwz_add',compact('domain'));
    }
    public function dwz_edit($id)
    {
       
        $dwzurl=DwzUrl::where('id',$id)->where('uid',session('user')->id)->first();
       
        $domain=Domain::where('id',$dwzurl->domain_id)->first();
        return view('user.dwz_edit',compact('dwzurl','domain'));
    }
    public function dwz(Request $request ,$id)
    {
        $domain=Domain::where('id',$id)->first();
        if(!$domain){
            return '域名不存在';
        }
        if ($domain['share'] !=1) {
            return '该域名不共享';
        }
        if ($domain['status'] !=1) {
            return '该域名未审核';
        }
        return view('index.dwz',compact('domain'));
    }
    public function dwz_lists()
    {
        $dwzurl=DwzUrl::where('uid',session('user')->id)->paginate(10);
        return view('user.dwz_lists',compact('dwzurl'));
    }
    public function gxsc()
    { 
        $domain=Domain::where('status',1)->where('share',1)->get();
        return view('index.gxsc',compact('domain'));
    }
    //退出登录
    public function logout()
    {
        session()->forget('user');
        return redirect('/user');
    }
    //修改密码
    public function password_edit()
    {
        return view('user.password_edit');
    }
    public function password_reset(Request $request)
    {

        if ($request->isMethod('post')) {
            $email = $request->input('email');
            $pass = $request->input('password');
            $captcha = $request->input('code');

            // 验证邮箱和密码不能为空且密码长度大于6位
            if (empty($email) || empty($pass)) {
                return back()->with('danger', '邮箱和密码不能为空');
            }
            if (strlen($pass) < 6) {
                return back()->with('danger', '密码长度必须大于6位');
            }

            $storedCode = \Cache::get('verify_code_' . $email);

            if ($storedCode && $storedCode == $captcha) {
                // 验证码正确
               // 检查用户是否存在  
               $user = User::where('email', $email)->first();
               if (!$user) {
                   return back()->with('danger', '用户不存在');
               }

               // 更新密码
               $user->pass = hash::make($pass);
               $user->save();

               return back()->with('success', '密码重置成功');
            } else {
                // 验证码错误或过期
                return back()->with('danger', '验证码错误或过期');

            }
            
        }
        return view('user.password_reset');
    }
    
    
}